package com.yungu.swift.api.route.v1.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyMemberDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerUuidPageParam;
import com.yungu.swift.assets.passenger.model.vo.PassengerEmergencyVo;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyMemberService;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 行程安全-紧急联系人相关api
 *
 * @author Zerrion
 * @version 1.0.0-RELEASE
 * @since 2018-10-16 11:27
 */
@Controller
@RequestMapping(value = "/api/v1/passenger")
@Api(value = "【乘客】【行程安全】【紧急联系人】乘客紧急联系人通用接口", tags = "乘客紧急联系人相关操作")
public class EmergencyApi extends BaseApi {

    @Reference
    private PassengerEmergencyService passengerEmergencyService;
    @Reference
    private PassengerEmergencyMemberService passengerEmergencyMemberService;

    @Reference
    private PassengerService passengerService;


    @ApiOperation(value = "新增紧急联系成员接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/addMember"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<String> addMember(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "memberName", value = "成员姓名", required = true) @RequestParam String memberName,
            @ApiParam(name = "memberMobile", value = "成员电话", required = true) @RequestParam String memberMobile,
            HttpServletRequest request) {
        if (!ValidateUtils.validateMobile(memberMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        return passengerEmergencyService.addMember(getUserUid(request), memberName, memberMobile);
    }

    @ApiOperation(value = "删除紧急联系成员接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/delMember"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<String> delMember(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "emergencyUuid", value = "紧急联系UUID", required = true) @RequestParam String emergencyUuid,
            @ApiParam(name = "memberUuid", value = "成员UUID", required = true) @RequestParam String memberUuid) {
        return passengerEmergencyService.delMember(emergencyUuid, memberUuid);
    }

    @ApiOperation(value = "更新紧急联系成员信息接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/updateMember"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<String> updateMember(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "emergencyUuid", value = "紧急联系UUID", required = true) @RequestParam String emergencyUuid,
            @ApiParam(name = "memberUuid", value = "成员UUID", required = true) @RequestParam String memberUuid,
            @ApiParam(name = "memberName", value = "成员UUID", required = true) @RequestParam String memberName,
            @ApiParam(name = "memberMobile", value = "成员UUID", required = true) @RequestParam String memberMobile) {
        if (!ValidateUtils.validateMobile(memberMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        return passengerEmergencyService.updateMember(emergencyUuid, memberUuid, memberName, memberMobile);
    }


    @ApiOperation(value = "切换紧急自动分享行程信息接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/updateStatus"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Integer> updateStatus(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "emergencyUuid", value = "紧急联系UUID", required = true) @RequestParam String emergencyUuid,
            @ApiParam(name = "emergencyStatus", value = "紧急联系状态[1：是，0：否]", required = true) @RequestParam Integer emergencyStatus) {
        return passengerEmergencyService.updateStatus(emergencyUuid, emergencyStatus, null, null);
    }


    @ApiOperation(value = "切换紧急成员自动分享行程信息接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/updateMemberStatus"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Integer> updateMemberStatus(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "emergencyUuid", value = "紧急联系UUID", required = true) @RequestParam String emergencyUuid,
            @ApiParam(name = "memberUuid", value = "成员UUID", required = true) @RequestParam String memberUuid,
            @ApiParam(name = "memberStatus", value = "紧急联系状态[1：是，0：否]", required = true) @RequestParam Integer memberStatus) {
        return passengerEmergencyService.updateMemberStatus(emergencyUuid, memberUuid, memberStatus);
    }


    @ApiOperation(value = "紧急联系人是否设置接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/settingInfo"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Map<String, Object>> settingInfo(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        String passengerUuid = getUserUid(request);
        Map<String, Object> resultMap = MapUtils.build(2);
        PassengerEmergencyDto emergencyDto = passengerEmergencyService.get(MapUtils.build("passengerUuid", passengerUuid)).getData();
        List<PassengerEmergencyMemberDto> listMember = passengerEmergencyMemberService.list(MapUtils.build("passengerUuid", passengerUuid)).getData();
        if (null == emergencyDto) {
            resultMap.put("emergencyStatus", false);
        } else {
            resultMap.put("emergencyStatus", PassengerEmergencyDto.EMERGENCY_STATUS_INVALID != emergencyDto.getEmergencyStatus() ? true : false);
        }
        if (CollectionUtils.isEmpty(listMember)) {
            resultMap.put("isSet", false);
        } else {
            resultMap.put("isSet", true);
        }
        return ResponseData.buildSuccessResponse("紧急联系人是否设置获取成功", resultMap);
    }


    @ApiOperation(value = "紧急联系人详情信息接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/emergencyInfo"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<PassengerEmergencyVo> emergencyInfo(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        PassengerEmergencyVo emergencyBean = passengerEmergencyService.emergencyInfo(getUserUid(request)).getData();
        return ResponseData.buildSuccessResponse("紧急联系人详情信息获取成功", null == emergencyBean ? new PassengerEmergencyVo() : emergencyBean);
    }

    @ApiOperation(value = "修改紧急联系人是否默认", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/emergency/updateDefault"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Integer> updateDefault(
            @ApiParam(name = "memberUuid", value = "紧急联系人uuid", required = true) @RequestParam String memberUuid,
            @ApiParam(name = "token", value = "token", required = true) @RequestParam String token,
            HttpServletRequest request) {
        ResponseData<Map<String, String>> responseData = passengerService.getPassengerCachedByToken(token);
        AdminPassengerUuidPageParam passengerUuidPageParam = new AdminPassengerUuidPageParam();
        passengerUuidPageParam.setUuid(memberUuid);
        passengerUuidPageParam.setPassengerUuid(responseData.getData().get("uuid"));
        return passengerEmergencyMemberService.updateIsDefault(passengerUuidPageParam);
    }
}
